package chapter14;

import java.sql.*;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-26 上午11:48
 */
public class BatchTest {
    public static void main(String[] args) {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://192.168.100.66/java_test?useSSL=false&serverTimezone=UTC&amp";
        String userName = "root";
        String password = "crscd123@";

        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        try (Connection conn = DriverManager.getConnection(url, userName, password)) {
            Statement stmt = conn.createStatement();
            String sql_insert = "INSERT INTO book (name, price) VALUES ('test1',123.3);";
            stmt.addBatch(sql_insert);
            stmt.addBatch(sql_insert);
            stmt.addBatch(sql_insert);
            stmt.addBatch(sql_insert);
            stmt.addBatch(sql_insert);
            int[] result = stmt.executeBatch();
            System.out.println("result:" + Arrays.toString(result));

            String sql_insert2 = "INSERT INTO book (name, price, pubdate) VALUES (?,?,?);";
            PreparedStatement statement = conn.prepareStatement(sql_insert2);
            for (int i = 0; i < 10; i++) {
                statement.setString(1, "test3");
                statement.setDouble(2, 313.34);
                statement.setDate(3, new Date(new java.util.Date().getTime()));
                statement.addBatch();
            }

            int[] r = statement.executeBatch();
            System.out.println("batch:" + Arrays.toString(r));

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
